
const { app, BrowserWindow, Menu, Tray } = require('electron')
const path = require('path')
let win
app.whenReady().then(()=>{
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      webviewTag: true // 开启webview标签
    }
  })
  win.loadFile(path.join(__dirname,'../renderer/index.html'))
  // 打印当前页面的所有frames信息（framesInSubtree方法）
  setTimeout(printFrames,3000)
})
function printFrames() {
  const frames = win.webContents.mainFrame.framesInSubtree
  const print = (frame) => frame && frame.url && path.basename(frame.url)
  frames.forEach((it) => {
    console.log(`current frame: ${print(it)}`)
    console.log(`   children: ${JSON.stringify(it.frames.map((it) => print(it)))}`)
    console.log(`   parent`, print(it.parent), '\n')
  })
}

/*
 * index.html加载webview标签结果：
  current frame: index.html
  children: []
  parent null 

  current frame: cn.bing.com
  children: []
  parent null 
 * 
 */

  /*
 * index.html加载iframe标签结果：
   current frame: index.html
   children: ["cn.bing.com"]
   parent null 

   current frame: cn.bing.com
   children: []
   parent index.html
 * 
 */
/* 结论：webview标签没有父iframe,本身就是一个mainFrame; iframe的父iframe本身是包含iframe标签的。*/